import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { ApiService } from './../core/api/api.service';
import { ToptipsService } from 'ngx-weui';
import { I18nService } from '../core/i18n/i18n.service';
import { StorageService } from '../core/sotrage/storage.service';

@Component({
	templateUrl: './updatePwd.html',
	styleUrls: ['./updatePwd.less'],
})
export class UpdatePwdComponent implements OnInit {
	data: any = {
		email: null,
		verifyCode:'',
		newPwd:'',
		reLoginPw:''
	};
	active = 1;
	type;
	disabled = false;
	userDel;
	title;
	iData;
	constructor(
		private sto: StorageService, 
		private i: I18nService, 
		private route: Router, 
		private activatedRoute: ActivatedRoute, 
		private apiService: ApiService, 
		private srv: ToptipsService
	) {
		this.i.get([
			'MSG.register-code',
			'MSG.dlmmok',
			'registerPage.input-mailCode-error',
			'updatePwd-page.xgdlmm',
			'updatePwd-page.xgzfmm',
			'updatePwd-page.wjmm',
			'error.yxyzm',
			'error.dlmm',
			'error.zfmm',
			'error.redlmm',
			'error.rezfmm',
			'MSG.zfmmok'
		]).subscribe(res => {
			this.iData = res;
			this.activatedRoute.params.subscribe((params: Params) => {
				this.type = Number(params['type']);
				this.title = this.type === 1 ? this.iData['updatePwd-page.xgdlmm'] : this.type === 2 ? this.iData['updatePwd-page.xgzfmm'] : this.iData['updatePwd-page.wjmm'];
				if (this.type !== 3) {
					this.userDel = this.sto.get('user');
					this.data.email = this.userDel.loginId;
					// this.apiService.post('用户详情', {}).then(res => {
					// 	this.userDel = res['data'];
					// 	this.data.email = this.userDel.email;
					// });
				}
			});
		});
	}
	ngOnInit() {
	}
	showThis(i) {
		if (this.active !== i) {
			this.active = i;
		}
	}
	back() {
		if (this.type == 3) {
			this.route.navigateByUrl('/login');
		} else {
			this.route.navigateByUrl('/security');
		}
	}
	sendCode(e) {
		const element = e.target;
		if (this.data.email) {
			if (!this.disabled) {
				let url = this.type === 2? '发送验证码' : '发送验证码(未登录)';
				let typeMsg = this.type === 2? 'tradepw' : 'repw';
				this.disabled = true;
				this.apiService
					.post(url, { email: this.data.email, type: typeMsg })
					.then(res => {
						this.srv.success(this.iData['MSG.register-code']);
						let t = 60;
						element.innerHTML = t + 's' + this.iData['registerPage.input-mailCode-error'];
						const i = setInterval(() => {
							t--;
							element.innerHTML = t + 's' + this.iData['registerPage.input-mailCode-error'];
							if (t < 1) {
								clearInterval(i);
								this.disabled = false;
							}
						}, 1000);
					})
			}
		}
	}
	sub() {
		if (!this.data.verifyCode) {
			this.srv.warn(this.iData['error.yxyzm']);
			return;
		}
		if (this.type === 1 || this.type === 3) {
			if (!this.data.newPwd) {
				this.srv.warn(this.iData['error.dlmm']);
				return;
			}
			if (!this.data.reNewPwd) {
				this.srv.warn(this.iData['error.redlmm']);
				return;
			}
			let url = this.type == 1 ? '修改登入密码(未登入)' : '修改登入密码(未登入)';
			this.apiService.post(url, this.data).then(res => {
				this.srv.success(this.iData['MSG.dlmmok']);
				this.type == 1 ? this.route.navigateByUrl('/indexMy') : this.route.navigateByUrl('/login');
			});
		} else if (this.type === 2) {
			if (!this.data.tradePw) {
				this.srv.warn(this.iData['error.zfmm']);
				return;
			}
			if (!this.data.reTradePw) {
				this.srv.warn(this.iData['error.rezfmm']);
				return;
			}
			this.apiService.post('修改支付密码', this.data).then(res => {
				this.srv.success(this.iData['MSG.zfmmok']);
				this.route.navigateByUrl('/indexMy');
			});
		}

	}
}
